<?php
/**
 * Created by IntelliJ IDEA.
 * User: xingshi
 * Date: 16/8/21
 * Time: 下午5:13
 */

namespace App\Http\Controllers;


use App\Http\Requests;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;
use Crypt;
use App\Resume;
use App\Eduexp;
use App\Workexp;
use Validator;
use App\User;
use App\Utils;
use App\Permission;

class resumeController extends Controller
{
    static $URL = '/';
    public function __construct()
    {
        //          $this->middleware('auth');
    }

    public function index(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

		$resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

        $eduexps2 = Eduexp::whereRaw("userid = ?", [$userid])->get();
		foreach($eduexps2 as $eduexp){
			if(count($resume)>0 && $resume->updated_at < $eduexp->updated_at) $resume->updated_at = $eduexp->updated_at;
		}
        $workexps2 = Workexp::whereRaw("userid = ?", [$userid])->get();
		foreach($workexps2 as $workexp){
			if(count($resume)>0 && $resume->updated_at < $workexp->updated_at) $resume->updated_at = $workexp->updated_at;
		}


		$data = DB::select("select count(*) as all_cnt from invitations where status>-2 and isdelete=0 and userid=".$userid." order by updated_at desc");
        $all_cnt = $data[0]->all_cnt;

        $data = DB::select("select count(*) as not_cnt from invitations where status=1 and isdelete=0 and userid=".$userid." order by updated_at desc");
        $not_cnt = $data[0]->not_cnt;

        $data = DB::select("select count(*) as interview_cnt from invitations where (status=210 or status=211 or status=214) and isdelete=0 and userid=".$userid." order by updated_at desc");
        $interview_cnt = $data[0]->interview_cnt;

        $data = DB::select("select count(*) as result_cnt from invitations where (status=2121 or status=2120 or status=213 or status=2122) and isdelete=0 and userid=".$userid." order by updated_at desc");
        $result_cnt = $data[0]->result_cnt;

        return View::make("resume.set")->with(compact("resume"))->with(compact("eduexps"))->with(compact("workexps"))->with(compact("all_cnt"))->with(compact("not_cnt"))->with(compact("interview_cnt"))->with(compact("result_cnt"))->with(compact("userid2"))->with(compact("isself"));
    }

	public function preview(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

        $eduexps2 = Eduexp::whereRaw("userid = ?", [$userid])->get();
		foreach($eduexps2 as $eduexp){
			if(count($resume)>0 && $resume->updated_at < $eduexp->updated_at) $resume->updated_at = $eduexp->updated_at;
		}
        $workexps2 = Workexp::whereRaw("userid = ?", [$userid])->get();
		foreach($workexps2 as $workexp){
			if(count($resume)>0 && $resume->updated_at < $workexp->updated_at) $resume->updated_at = $workexp->updated_at;
		}
        $user = User::find($userid);

		$data = DB::select("select count(*) as all_cnt from invitations where status>-2 and isdelete=0 and userid=".$userid." order by updated_at desc");
        $all_cnt = $data[0]->all_cnt;

        $data = DB::select("select count(*) as not_cnt from invitations where status=1 and isdelete=0 and userid=".$userid." order by updated_at desc");
        $not_cnt = $data[0]->not_cnt;

        $data = DB::select("select count(*) as interview_cnt from invitations where (status=210 or status=211 or status=214) and isdelete=0 and userid=".$userid." order by updated_at desc");
        $interview_cnt = $data[0]->interview_cnt;

        $data = DB::select("select count(*) as result_cnt from invitations where (status=2121 or status=2120 or status=213 or status=2122) and isdelete=0 and userid=".$userid." order by updated_at desc");
        $result_cnt = $data[0]->result_cnt;

        return View::make("resume.preview")->with(compact("user"))->with(compact("resume"))->with(compact("eduexps"))->with(compact("workexps"))->with(compact("all_cnt"))->with(compact("not_cnt"))->with(compact("interview_cnt"))->with(compact("result_cnt"))->with(compact("userid2"))->with(compact("isself"))->with(compact("resumeisok"));
    }

	public function save_avatar_url(Request $request){
        $path = self::upload($request);
        $userid = Auth::user()->id;
		$resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        if(count($resume) == 0){
            $input['userid'] = $userid;
            $input['avatar_url'] = $path;
            Resume::create($input);
        }
        else {
            $resume->avatar_url = $path;
            $resume->save();
        }


		$res = array();
		$res['suc'] = true;
        $res['data'] = $path;
        return response()->json($res);
    }

    public function save_resume(Request $request){
        $input = $request->all();
        $userid = Auth::user()->id;
		$resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        if(count($resume) == 0){
            $input['userid'] = $userid;
            Resume::create($input);
        }
        else {
            $resume->realname = $input['realname'];
            $resume->sex = $input['sex'];
            $resume->birthday = $input['birthday'];
            $resume->working_years = $input['working_years'];
            $resume->in_province = $input['in_province'];
            $resume->in_city = $input['in_city'];
            $resume->phone_number = $input['phone_number'];
            $resume->expect_city = $input['expect_city'];
            $resume->expect_job = $input['expect_job'];
            $resume->save();
        }

		
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		$res=array();
		$res['suc'] = true;
        $res['resumeisok'] = $resumeisok;
        return response()->json($res);
    }

    public function save_edu(Request $request){
		$input = $request->all();
		$userid = Auth::user()->id;
		$input['userid'] = $userid;
		$input['isdelete'] = 0;
		$data = Eduexp::create($input);

		
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		unset($data['userid']);
		$res=array();
		$res['suc'] = true;
		$res['data'] = $data;
        $res['resumeisok'] = $resumeisok;
        return response()->json($res);
    }

	public function update_edu(Request $request){
        $input = $request->all();
		$eduexp = Eduexp::find($input['id']);
		$eduexp->school = $input['school'];
		$eduexp->edu_bg = $input['edu_bg'];
		$eduexp->begin_at = $input['begin_at'];
		$eduexp->end_at = $input['end_at'];
		$eduexp->major = $input['major'];
		$eduexp->save();

		
		$userid = Auth::user()->id;	
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		unset($eduexp['userid']);
		$res=array();
		$res['suc'] = true;
		$res['data'] = $eduexp;
        $res['resumeisok'] = $resumeisok;
        return response()->json($res);
	}

	public function del_edu(Request $request){
		$input = $request->all();
        $eduexp = Eduexp::find($input['id']);
        $eduexp->isdelete = 1;
        $eduexp->save();

		
		$userid = Auth::user()->id;	
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		$res=array();
		$res['suc'] = true;
        $res['resumeisok'] = $resumeisok;

        return response()->json($res);
	}

	public function save_work(Request $request){
        $input = $request->all();
		$userid = Auth::user()->id;
        $input['userid'] = $userid;
        $input['isdelete'] = 0;
        $data = Workexp::create($input);

		
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;


        unset($data['userid']);
        $res = array();
        $res['suc'] = true;
        $res['data'] = $data;
        $res['resumeisok'] = $resumeisok;
        return response()->json($res);
    }

    public function update_work(Request $request){
        $input = $request->all();
        $workexp = Workexp::find($input['id']);
        $workexp->company = $input['company'];
        $workexp->job = $input['job'];
        $workexp->begin_at = $input['begin_at'];
        $workexp->end_at = $input['end_at'];
        $workexp->exp = $input['exp'];
        $workexp->save();

		
		$userid = Auth::user()->id;	
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		unset($workexp['userid']);
        $res = array();
		$res['suc'] = true;
		$res['data'] = $workexp;
        $res['resumeisok'] = $resumeisok;
        return response()->json($res);
    }

    public function del_work(Request $request){
        $input = $request->all();
        $workexp = Workexp::find($input['id']);
        $workexp->isdelete = 1;
        $workexp->save();

		$userid = Auth::user()->id;	
        $resume = Resume::whereRaw("userid = ?", [$userid])->get()->first();
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();

		$resumeisok = false;
		if(count($resume)>0 && count($eduexps)>0 && count($workexps)>0) $resumeisok = true;

		$res=array();
		$res['suc'] = true;
        $res['resumeisok'] = $resumeisok;

        return response()->json($res);
    }

    private function upload($request)
    {
		$file = array('image' => $request->file('avatar_url'));
        $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return false;
        }

        if ($request->file('avatar_url')->isValid()) {
            $destinationPath = self::getrootdir(); // upload path
            $extension = $request->file('avatar_url')->getClientOriginalExtension(); // getting image extension
            $fileName = time() . rand(11111, 99999) . '.' . $extension; // renameing image
            $request->file('avatar_url')->move($destinationPath, $fileName); // uploading file to given path
            $path = self::$URL . $destinationPath . '/' . $fileName;
            return $path;
        }
        else return "";
    }

    private function getrootdir()
    {
        $email = Auth::user()->email;
        $emailpath = substr(md5($email), 0, 10);
        $destinationPath = 'uploads/avatar_url_' . $emailpath; // upload path
        if (!is_dir($destinationPath)) mkdir($destinationPath);
        return $destinationPath;
    }

}
